import requests
import os
from utils.logger_util import logger

# 支持通过环境变量 API_BASE_URL 配置基础 URL，默认为本地地址
API_BASE_URL = os.getenv("API_BASE_URL", "http://127.0.0.1:8080/")
host = API_BASE_URL if API_BASE_URL.endswith("/") else API_BASE_URL + "/"

class Request:
    log  = logger.getlog()

    def get(self,url,**kwargs):
        self.log.info("准备发起get请求，url:"+url)
        self.log.info("接口信息：{}".format(kwargs))

        r = requests.get(url=url,**kwargs)

        self.log.info("接口响应状态码：{}".format(r.status_code))
        self.log.info("接口响应内容：{}".format(r.text))

        return r

    def post(self,url,**kwargs):
        self.log.info("准备发起post请求，url:"+url)
        self.log.info("接口信息：{}".format(kwargs))
        self.log.info(f"post请求的url：{url}")
        r=None
        try:
            r = requests.post(url=url, **kwargs)
        except Exception as e:
            self.log.error(f"post请求出现异常：{e}")

        self.log.info("接口响应状态码：{}".format(r.status_code))
        self.log.info("接口响应内容：{}".format(r.text))

        return r